<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <form id="form" onsubmit="return false;">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="info" placeholder="昵称/手机号码" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list layui-btn-primary" lay-submit lay-filter="LAY-app-contlist-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
                        </button>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-list layui-btn-primary" data-type="export">导出</button>
                    </div>
                </div>
            </form>

        </div>

        <div class="layui-card-body">
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
            <script type="text/html" id="table-content-list">
<!--                {if check_roles('admin/topic/edit')}-->
<!--                <a class="layui-btn layui-btn-normal-outline layui-btn-xs" lay-event="edit">编辑</a>-->
<!--                {/if}-->
                {if check_roles('admin/parents/delete') }
                <a class="layui-btn layui-btn-danger-outline layui-btn-xs" lay-event="del">删除</a>
                {/if}
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['table','form'],function () {
        var $ = layui.$,table = layui.table,form = layui.form;

        //表单
        table.render({
            elem: '#LAY-app-content-list'
            ,url: "{:url('parents/index')}"
            ,cols: [[
                 {field: 'image_label',  title: '头像'}
                ,{field: 'nickname',  title: '昵称'}
                ,{field: 'mobile',  title: '联系方式'}
                ,{field: 'gender_alias',  title: '性别'}
                ,{field: 'create_time',  title: '注册时间'}
                ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
            ]]
            ,page: true
            ,size: 'lg'
        });

        //搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            table.reload('LAY-app-content-list', {
                where: field
                ,page:1
            });
        });


        //工具条
        table.on('tool(LAY-app-content-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除['+data['nickname']+']？', function(index){
                    $.post("{:url('parents/delete')}",{id:data.id},function (ret) {
                        if(ret.code == 0){
                            layer.msg(ret.msg,{icon:6,time:1000},function () {
                                layui.table.reload('LAY-app-content-list');
                            })
                        }else{
                            layer.msg(ret.msg,{icon:5,time:2000})
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,content: "{:url('topic/edit')}?id="+data.id
                    ,shadeClose: true
                    ,shade: 0.4
                    ,area: ['500px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            }
        });


        //顶部按钮
        var active = {
            add: function(){
                var index = layer.open({
                    type: 2
                    ,title: '添加'
                    ,content: "{:url('topic/add')}"
                    ,maxmin: false
                    ,area: ['500px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                        submit.click();
                    }
                });
            },
            export:function () {
                var data = $("#form").serializeArray()
                    ,url = "{:url('parents/export')}?";
                for (var i in data){
                    if (data[i]['value'] != ''){
                        url += "&"+data[i]['name']+"="+data[i]['value']
                    }
                }
                location.href = url
            },
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });

</script>
